/**
 * 配置管理器
 */
import * as vscode from 'vscode';
import { ExtensionConfig } from './types';

/**
 * 配置管理器类
 */
export class ConfigManager {
    private static readonly SECTION = 'phpZeroRefHighlighter';

    /**
     * 获取扩展配置
     */
    public static getConfig(): ExtensionConfig {
        const config = vscode.workspace.getConfiguration(this.SECTION);

        return {
            enabled: config.get<boolean>('enabled', true),
            opacity: Math.max(0.1, Math.min(1.0, config.get<number>('opacity', 0.5))),
            throttleDelay: Math.max(100, config.get<number>('throttleDelay', 500)),
            maxRetries: Math.max(0, Math.min(10, config.get<number>('maxRetries', 3))),
            enableDebugLog: config.get<boolean>('enableDebugLog', false),
            excludePatterns: config.get<string[]>('excludePatterns', [
                '__construct',
                '__destruct',
                '__get',
                '__set',
                '__call',
                '__callStatic',
                '__isset',
                '__unset',
                '__toString',
                '__invoke',
                '__clone'
            ])
        };
    }

    /**
     * 监听配置变化
     */
    public static onConfigChanged(callback: (config: ExtensionConfig) => void): vscode.Disposable {
        return vscode.workspace.onDidChangeConfiguration(event => {
            if (event.affectsConfiguration(this.SECTION)) {
                callback(this.getConfig());
            }
        });
    }

    /**
     * 检查函数名是否应该被排除
     */
    public static shouldExcludeFunction(functionName: string, excludePatterns: string[]): boolean {
        return excludePatterns.some(pattern => {
            try {
                const regex = new RegExp(pattern, 'i');
                return regex.test(functionName);
            } catch {
                // 如果正则表达式无效，则进行简单的字符串匹配
                return functionName.toLowerCase().includes(pattern.toLowerCase());
            }
        });
    }
}